package com.sweet.thread;


import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableDownload implements Callable<Boolean> {
    private String url;
    private String name;

    public CallableDownload(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public Boolean call() {
        WebDownloader load = new WebDownloader();
        load.download(url, name);
        System.out.println("下载了文件：" + name);
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallableDownload t1 = new CallableDownload("https://inews.gtimg.com/newsapp_bt/0/11181809205/1000", "1.jpg");
        CallableDownload t2 = new CallableDownload("https://inews.gtimg.com/newsapp_bt/0/11181809206/1000", "2.jpg");
        CallableDownload t3 = new CallableDownload("https://inews.gtimg.com/newsapp_bt/0/11181809207/1000", "3.jpg");
        CallableDownload t4 = new CallableDownload("https://inews.gtimg.com/newsapp_bt/0/11181809209/1000", "4.jpg");
        CallableDownload t5 = new CallableDownload("https://inews.gtimg.com/newsapp_bt/0/11181809527/1000", "5.jpg");

        // 1.创建执行服务
        ExecutorService exr = Executors.newFixedThreadPool(5);
        // 2.提交执行
        Future<Boolean> r1 = exr.submit(t1);
        Future<Boolean> r2 = exr.submit(t2);
        Future<Boolean> r3 = exr.submit(t3);
        Future<Boolean> r4 = exr.submit(t4);
        Future<Boolean> r5 = exr.submit(t5);
        // 3.获取结果
        System.out.println(r1.get());
        System.out.println(r2.get());
        System.out.println(r3.get());
        System.out.println(r4.get());
        System.out.println(r5.get());
        // 4.关闭服务
        exr.shutdownNow();
    }
}


